package com.ebelter.btcomlib.models.bluetooth.products.three2one;

import android.content.Context;
import android.os.Message;
import com.ebelter.btcomlib.models.bluetooth.products.three2one.bean.ThreeOneMesureResultBean;
import com.ebelter.btcomlib.models.bluetooth.products.three2one.interfaces.ThreeOneMesureResult;
import com.ebelter.btcomlib.utils.BaseHandle;
import com.ebelter.btcomlib.utils.BytesUtils;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import java.util.ArrayList;


public class ThreeOneBytesAnalysis extends BaseHandle{

    private static final String TAG = "ThreeOneBytesAnalysis";
    private ThreeOneMesureResult threeOneMesureResult;


    public ThreeOneBytesAnalysis(Context context) {
        super(context);
        resultLists=new ArrayList<>();
    }

    // 数据是否已结束
    public boolean isEendDataStop = false;




    private static final int WHAT_MESURE_OVER = 1;


    @Override
    public void handleMsg(Message msg) {
        super.handleMsg(msg);
        if (msg.what==WHAT_MESURE_OVER){
            isEendDataStop = false;
            resultLists.clear();
        }
    }

    public void clear(){
        resultLists.clear();
    }


    private String lastMesureTime;

    private ArrayList<byte[]> resultLists;

    public void resultAnalysis(byte[] data) {

        if (threeOneMesureResult==null){
            return;//如果回调接口都是null的话就没必要往下解析了 解析了页回调不出去 直接reture掉 省点cpu性能
        }
        if (data.length!=13){
            LogUtils.i(TAG,"接收到的数据不符合要求---被return了");
            return;
        }
        LogUtils.i(TAG,"ThreeOneBytesAnalysisResultAnalysis---"+ BytesUtils.bytes2HexStr(data));

        removeMessages(WHAT_MESURE_OVER);//这一段代码的意思是 如果2秒钟都没有来测量数据 默认这次测量结束了
        sendEmptyMessageDelayed(WHAT_MESURE_OVER,2000);


        if (isEendDataStop){
            LogUtils.i(TAG,"此次数据已经接收完成---被return了");
            return;
        }

        if ((data[0] & 0xff) == 0x55&&(data[1] & 0xff) == 0x55&&(data[2] & 0xff) == 0x55) {       // 此次已发送完毕的意思
            LogUtils.i(TAG,"此次数据已经接收完成----被reture");
            isEendDataStop = true;
            resultLists.clear();
            return;
        }

        if (resultLists.size()<2){
            resultLists.add(data);
            LogUtils.i(TAG,"resultLists.size()小于2 resultLists增加了这条数据");
        }

        if (resultLists.size()!=2){
            LogUtils.i(TAG,"resultLists.size()!=2----被reture");
            return;
        }


        data=resultLists.get(1);
        int secondByte = data[1] & 0xff;
        int cmdCode = secondByte >> 6;

        if (secondByte == 0x5a || secondByte == 0xa5) {   // 判断测量值是否过高过低
            int error = 0;
            switch (secondByte) {
                case 0x5a: //Lo 过低
                    LogUtils.i(TAG, "三合一测量值过低");
                    error = 1;
                    break;
                case 0xa5: //Hi 过高
                    LogUtils.i(TAG, "三合一测量值过高");
                    error = 2;
                    break;
            }


            final int finalError = error;
            if (threeOneMesureResult!=null)threeOneMesureResult.error(finalError);



        } else {
            LogUtils.i(TAG, "三合一测量类型 = " +  Integer.toHexString(cmdCode));
            String mt = TimeUtils.formatTime1(System.currentTimeMillis());
            if (mt.equals(lastMesureTime)){
                LogUtils.i(TAG, "因为与上一次的测量时间一致-------被reture");//精确到秒数
            }
            lastMesureTime = mt;
            switch (cmdCode) {
                case 0x01: //血糖测量结果
                    doGluResult(data);
                    break;
                case 0x02: //尿酸测量结果
                    doUaResults(data);
                    break;
                case 0x03: //胆固醇测量结果
                    doCholResult(data);
                    break;
            }
        }



    }



    //胆固醇测量结果
    private void doCholResult(byte[] data) {

        int chol = data[1] & 0xff & 0x3f;
        chol = chol << 8 | (data[2] & 0xff);

        float newChol = chol / 100.0f;
        LogUtils.i(TAG, "--胆固醇测量结果---doUaResults------chol = " + newChol);

        long measureTime = System.currentTimeMillis();
        final ThreeOneMesureResultBean result=new ThreeOneMesureResultBean(3,newChol,measureTime);

        if (threeOneMesureResult!=null)threeOneMesureResult.mesureResult(result);



    }

    //尿酸测量结果
    private void doUaResults(byte[] data) {

        int ua = data[1] & 0xff & 0x3f;
        ua = ua << 8 | (data[2] & 0xff);

        LogUtils.i(TAG, "--尿酸测量结果---doUaResults------ ua = " + ua);

        long measureTime = System.currentTimeMillis();


        final ThreeOneMesureResultBean result=new ThreeOneMesureResultBean(2,ua,measureTime);

         if (threeOneMesureResult!=null)threeOneMesureResult.mesureResult(result);



    }

    //血糖测量结果
    private void doGluResult(byte[] data) {
        int glu = data[1] & 0xff & 0x3f;
        glu = glu << 8 | (data[2] & 0xff);

        float newGlu = glu / 10.0f;
        LogUtils.i(TAG, "--血糖测量结果--doGluResult--newGlu= " + newGlu);

        long measureTime = System.currentTimeMillis();


        final ThreeOneMesureResultBean result=new ThreeOneMesureResultBean(1,newGlu,measureTime);

        if (threeOneMesureResult!=null)threeOneMesureResult.mesureResult(result);



    }

    void setThreeOneMeasureCallback(ThreeOneMesureResult threeOneMesureResult) {
        this.threeOneMesureResult=threeOneMesureResult;
    }


}
